import 'package:ac/api/ac_urls.dart';
import 'package:ac/data/ac_user_manager.dart';
import 'package:ac/model/ac_blocked_model.dart';
import 'package:get/get.dart';

import 'ac_blocked_list_state.dart';

class AcBlockedListLogic extends GetxController {
  final AcBlockedListState state = AcBlockedListState();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    _initData();
  }

  Future<void> _initData() async {
    var res = await AcUrls.blockList.post();
    if (res?["code"] == 0) {
      state.blockList.value =
          (res["data"] as List).map((e) => AcBlockedModel.fromJson(e)).toList();
    }
  }

  Future<void> unblockUser(String broadcasterId) async {
    var res = await AcUrls.removeBlock.post(data: {
      "blockUserId": broadcasterId,
    });
    if (res?["code"] == 0) {
      state.blockList
          .removeWhere((element) => element.broadcasterId == broadcasterId);
      var userInfo = AcUserManager.getUserInfo(broadcasterId);
      if (userInfo != null) {
        userInfo.isBlock = false;
        AcUserManager.updateBloggerUserInfo(userInfo);
        AcUserManager.updateVideoUserInfo(userInfo);
      }
    }
  }
}
